package io.renren.modules.sys.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.renren.common.utils.Constant;
import io.renren.common.utils.MapUtils;
import io.renren.modules.sys.dao.SysCollegeDao;
import io.renren.modules.sys.entity.SysCollegeEntity;
import io.renren.modules.sys.service.SysCollegeService;
import io.renren.modules.sys.service.SysLaboratoryService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

@Service("sysCollegeService")
public class SysCollegeServiceImpl extends ServiceImpl<SysCollegeDao, SysCollegeEntity> implements SysCollegeService {

    @Autowired
    private SysLaboratoryService sysLaboratoryService;

    /**
     * 根据父级Id查询学院信息
     * @param parentId
     * @param collegeIdList
     * @return
     */
    @Override
    public List<SysCollegeEntity> queryListParentId(Long parentId, List<Long> collegeIdList) {
        List<SysCollegeEntity> collegeList = queryListParentId(parentId);
        if(collegeIdList == null){
            return collegeList;
        }

        List<SysCollegeEntity> labcollegeList = new ArrayList<>();
        for(SysCollegeEntity college : collegeList){
            if(collegeIdList.contains(college.getCollegeId())){
                labcollegeList.add(college);
            }
        }
        return labcollegeList;
    }

    /**
     * 根据父菜单ID查询学院列表
     * @param parentId 父菜单ID
     * @return
     */
    @Override
    public List<SysCollegeEntity> queryListParentId(Long parentId) {
        return baseMapper.queryListParentId(parentId);
    }

    /**
     * 查询所有学院信息
     * @return
     */
    @Override
    public List<SysCollegeEntity> queryListCollege() {
        return baseMapper.queryListCollege();
    }

    /**
     * 删除学院信息
     * @param collegeIds
     */
    @Override
    public void deleteCollege(Long[] collegeIds) {
        //删除学院信息
        this.removeByIds(Arrays.asList(collegeIds));
    }

    /**
     * 获取所有学院列表
     * @param collegeIdList
     * @return
     */
    private List<SysCollegeEntity> getAllCollegeList(List<Long> collegeIdList){
        //查询根菜单列表
        List<SysCollegeEntity> collegeList = queryListParentId(0L, collegeIdList);
        //递归获取子菜单
        getCollegeTreeList(collegeList, collegeIdList);

        return collegeList;
    }

    /**
     * 递归
     * @param collegeList
     * @param collegeIdList
     * @return
     */
    private List<SysCollegeEntity> getCollegeTreeList(List<SysCollegeEntity> collegeList, List<Long> collegeIdList){
        List<SysCollegeEntity> subCollegeList = new ArrayList<SysCollegeEntity>();

        for(SysCollegeEntity entity : collegeList){
            //一级学院
            if(entity.getLevel() == Constant.CollegeLevel.FIRST.getValue()){
                entity.setList(getCollegeTreeList(queryListParentId(entity.getCollegeId(), collegeIdList), collegeIdList));
            }
            subCollegeList.add(entity);
        }

        return subCollegeList;
    }
}
